﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using WindowsFormsApp4.DLL;

namespace WindowsFormsApp4
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        DbUse db = new DbUse();
        private void MainForm_Load(object sender, EventArgs e)
        {

            DataSet ds = db.GetArticles();

            dataGridView1.DataSource = ds.Tables[0];

            dataGridView1.ReadOnly = true;  //使表只读不可修改 此时单击将只选择一个单元格
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  //通过单击某个单元格将选择整行
            dataGridView1.AllowUserToAddRows = false;   //去除表格中底部一行


            //列宽铺满dataGridView1
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void refData() 
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("Title", this.textBox1.Text.Trim());
            dic.Add("Author", this.textBox1.Text.Trim());

            DataSet ds = db.GetArticlesByTitleOrAuthor(new string[] { "*" }, "Articles", dic);

            dataGridView1.DataSource = ds.Tables[0];
        } 

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            refData();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editform = new EditForm(0,"","","");
            var res = editform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                refData();
            }          
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

                EditForm editform = new EditForm(Id, Title, Author, CreatedTime);
                var res = editform.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    refData();
                }

            }
            else
            {
                MessageBox.Show("您未选择任何数据行", "提示");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("没有选择任何行","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            var row = dataGridView1.SelectedRows[0];
            var Id = (int)row.Cells["Id"].Value;

            if (Id > 0)
            {
                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res.Equals(DialogResult.Yes))
                {          
                    var rowCount = db.DeleteArticle(Id);
                    if (rowCount > 0)
                    {
                        //更新当前数据
                        refData();
                    }
                }

            }
        }

        /// <summary>
        /// 窗口关闭时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
                Application.Exit();
        }
         
        /// <summary>
        /// 去用户界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            UserForm userForm = new UserForm();
            userForm.ShowDialog();
            this.Hide();
        }
    }
}
